package com.mlz.mlzmall.ui.utils;

import android.app.Activity;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.JsonRequest;
import com.mlz.mlzmall.ui.activity.act.main.MyApplication;
import com.mlz.mlzmall.ui.inter.NetResponses;
import com.mlz.mlzmall.ui.utils.mlz.ConfigInfo;
import com.mlz.mlzmall.ui.utils.mlz.URL;
import com.mlz.mlzmall.ui.utils.util.SharedUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;
import java.util.TimerTask;

/**
 * Created by Administrator on 2016/3/18.
 */
public class TimerUtils extends TimerTask implements NetResponses {

    private Activity context;

    public TimerUtils(Activity context) {
        super();
        this.context = context;
    }

    @Override
    public void run() {
        String sessionKey = (String) SharedUtils.get(context, ConfigInfo.SESSIONKEY, "");
        String phoneId = (String) SharedUtils.get(context, ConfigInfo.PHONEID, "");
        String url = URL.CUSTOMER.LOGIN_RETURN;
        String telephony = (String) SharedUtils.get(context, ConfigInfo.DEVICEID, "string");
        Map<String, String> map = new HashMap();
        map.put("telephone", phoneId);
        map.put("deviceId", telephony);
        map.put("randomKey", sessionKey);

        request(Request.Method.POST, url, map, sessionKey, "appLogin", 100);

    }

    public void request(int method, final String URL, Map map, final String session, String tag, final int type) {


        JSONObject jsonObject;
        if (method == Request.Method.POST)
            jsonObject = new JSONObject(map);
        else {
            jsonObject = null;
        }
        JsonRequest jsonRequest = new JsonObjectRequest(method, URL, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                TimerUtils.this.success(type, jsonObject);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                TimerUtils.this.fail();
            }
        }) {
            @Override
            public Map getHeaders() {
                HashMap headers = new HashMap();
                headers.put("Accept", "application/json");
                headers.put("Content-Type", "application/json");
                if (!"".equals(session) && session != null)
                    headers.put(ConfigInfo.COOKIE, ConfigInfo.COOKIE_KEY + session);

                return headers;
            }
        };
        // 设置该请求的标签
        jsonRequest.setTag(tag);
        // 将请求添加到队列中
        ((MyApplication) (context.getApplication())).getRequestQueue().add(jsonRequest);
    }

    @Override
    public void success(int type, JSONObject jsonObject) {
        String sessionKey = jsonObject.optString("data");
        System.out.println("==applogin=" + sessionKey);
        if (sessionKey != null) {
            SharedUtils.put(context, ConfigInfo.SESSIONKEY, sessionKey);
        }
    }

    @Override
    public void fail() {

    }
}
